package com.bristua.mall.user.controller;

import com.bristua.mall.common.bean.Result;
import com.bristua.mall.common.biz.ValidateCodeBiz;
import com.bristua.mall.common.enums.CommonCode;
import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.HandleResultUtil;
import com.bristua.mall.user.biz.UserLoginBiz;
import com.bristua.mall.user.biz.UserRegBiz;
import com.bristua.mall.user.model.condition.userReg.UserReg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/30.
 */
@RestController
@Api(value="用户登录",tags={"用户登录"})
public class UserLoginController {

    @Autowired
    private UserLoginBiz userLoginBiz;

    @Autowired
    private ValidateCodeBiz validateCodeBiz;

    @ApiOperation(value="用户登录")
    @PostMapping(value="/api/user/login")
    public Result userReg(@RequestBody UserReg userReg){
        if(StringUtils.isEmpty(userReg.getPhoneCode())){
            throw new BizException(CommonCode.VALIDATE_CODE_FAIL.getCode(),CommonCode.VALIDATE_CODE_FAIL.getMsg());
        }

        String s = validateCodeBiz.validateCode(userReg.getPhone(), userReg.getPhoneCode());
        if("0".equals(s)){
            Result result = new Result();
            Map<String,Object> map = new HashMap();
            map.put("token",userLoginBiz.userLogin(userReg));
            result.setData(map);
            result.setMsg(CommonCode.LOGIN_SUCCESS.getMsg());
            result.setCode(CommonCode.LOGIN_SUCCESS.getCode());
            validateCodeBiz.udpateCodeStatus(userReg.getPhone(),userReg.getPhoneCode());
            return result;
        }else{
            throw new BizException(CommonCode.VALIDATE_CODE_EXPIRE.getCode(),CommonCode.VALIDATE_CODE_EXPIRE.getMsg());
        }

    }
}
